//
//  NormalStockChildListController.m
//  Stock
//
//  Created by blacksun on 11-6-27.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "NormalStockChildListController.h"


@implementation NormalStockChildListController
@synthesize stockNewsService;
@synthesize loadMoreView;


#pragma mark -Service Delegate
-(void)requestFinishedWithResult:(id)aResult andUserinfo:(NSDictionary *)aUserinfo{
    [super requestFinishedWithResult:aResult andUserinfo:aUserinfo];
    [self.stockNewses addObjectsFromArray:aResult];
    [self.tableView reloadData];
    self.tableView.tableFooterView = self.loadMoreView;
}
-(void)requestFinishedwithError:(NSError *)aError andUserinfo:(NSDictionary *)aUserinfo{
    
}

-(void)queryStocknewses{
    self.tableView.tableFooterView = [[StockAppHelper shareInstance] getTableFooterView];
    NSString *queryUrl = [[NSString alloc] initWithFormat:@"%@report/list?sid=%@&start=%d",kHostName,sectionId,currentIndex-1];
    [stockNewsService queryStockNewsWithUrl:queryUrl ifNotExistInDBWithSectionId:sectionId andSectionIndex:currentIndex];
    [queryUrl release];
}
-(void)queryLastStockNewses{
    self.tableView.tableFooterView = [[StockAppHelper shareInstance] getTableFooterView];
    NSString *queryUrl = [[NSString alloc] initWithFormat:@"%@report/last?sid=%@&start=%d",kHostName,sectionId,currentIndex-1];
    NSLog(@"%@",queryUrl);
    [stockNewsService queryStockNewsWithUrl:queryUrl ifNotExistInDBWithSectionId:sectionId andSectionIndex:currentIndex];
    [queryUrl release];
}

-(void)viewDidLoad{
    [super viewDidLoad];
    self.stockNewses = [NSMutableArray array];
    currentIndex = sectionIndex + 1;
    self.tableView.bounces = NO;
    self.stockNewsService = [[[StockNewsService alloc] initWithDelegate:self] autorelease];
    
    UIView *footerView = [[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)] autorelease];
    footerView.backgroundColor = [UIColor blackColor];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:footerView.frame];
    imageView.image = [UIImage imageNamed:@"cellbg2.png"];
    [footerView addSubview:imageView];
    [imageView release];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(80.0, 2.0, 160.0, 40.0);
    [button setTitle:@"加载更多..." forState:UIControlStateNormal];
    [button addTarget:self action:@selector(queryStocknewses) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:button];
    
    self.loadMoreView = footerView;
    
    [self performSelector:@selector(queryLastStockNewses) withObject:nil afterDelay:0.2];
    /*if (!IsPaidVersion) {
        ADBannerView *banner = [[ADBannerView alloc] initWithFrame:CGRectMake(0.0, 366.0, 320.0, 50.0)];
        banner.delegate = self;
        [self.view insertSubview:banner belowSubview:self.tableView];
        [banner release];
        NSLog(@"ADBannerView");
    }*/
}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner{
    NSLog(@"bannerViewDidLoadAd");
    CGRect frame = self.tableView.frame;
    frame.size.height = frame.size.height - banner.bounds.size.height;
    //[UIView beginAnimations:nil context:nil];
    //[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    //[UIView setAnimationDuration:0.5];
    self.tableView.frame = frame;
    //[UIView commitAnimations];
    
    [self.view bringSubviewToFront:banner];
}

-(void)viewDidUnload{
    [super viewDidUnload];
    [stockNewsService cancleAllRequest];
    self.stockNewsService = nil;
    self.loadMoreView = nil;
}
-(void)dealloc{
    [self.stockNewsService cancleAllRequest];
    self.stockNewsService = nil;
    self.loadMoreView = nil;
    [super dealloc];
}

@end
